CREATE PROCEDURE [dbo].[AppointmentGet]
	@UBRN			INT,
	@PatientId		UNIQUEIDENTIFIER,
	@ReferrerId		UNIQUEIDENTIFIER,
	@ProviderId		UNIQUEIDENTIFIER,
	@Status			INT,
	@StartDateTime	DATETIME,
	@EndDateTime	DATETIME,
	@WorkflowId		UNIQUEIDENTIFIER
AS

SELECT	A.*
FROM	Appointment A
WHERE	(@UBRN			IS NULL OR A.UBRN = @UBRN)
	AND (@PatientId		IS NULL OR A.PatientId = @PatientId)
	AND (@ReferrerId	IS NULL OR A.ReferrerId = @ReferrerId)
	AND	(@ProviderId	IS NULL OR A.ProviderId = @ProviderId)
	AND	(@Status		IS NULL OR A.Status & @Status > 0)
	AND	(@StartDateTime	IS NULL OR ((DatePart(yy,A.StartDateTime) >= DatePart(yy,@StartDateTime)) AND (DatePart(dy,A.StartDateTime) >= DatePart(dy, @StartDateTime))))
	AND	(@EndDateTime	IS NULL OR ((DatePart(yy,A.EndDateTime) <= DatePart(yy,@EndDateTime)) AND (DatePart(dy,A.EndDateTime) <= DatePart(dy, @EndDateTime))))
	AND (@WorkflowId	IS NULL OR A.WorkflowId = @WorkflowId)
ORDER BY A.UBRN
	
RETURN @@ROWCOUNT